/**
 * Copyright (C) 2013 Oleg Anastassov <oleg.anastassov@gmail.com>
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kz.kstu.loc.desktop.dialogs;

import kz.kstu.loc.desktop.controls.factory.ComponentFactory;
import kz.kstu.loc.desktop.controls.factory.ImageFactory;
import kz.kstu.loc.desktop.icon.GUIIcon;
import kz.kstu.loc.desktop.settings.SettingsManager;
import kz.kstu.loc.util.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Date: 24.01.2013
 * Time: 10:24
 *
 * @author Oleg Anastassov
 */
public class SettingsDialog extends ADialog {

    private static final String TITLE = "Настройки :: Общие";

    private static final int WIDTH = 500;
    private static final int HEIGHT = 250;

    private JTextField prefixTextField;

    private JButton saveButton;
    private JButton cancelButton;

    @Override
    protected void initDialog() {
        setTitle(TITLE);
        setSize(new Dimension(WIDTH, HEIGHT));
        setIconImage(ImageFactory.createImage(GUIIcon.COMMONS));
    }

    @Override
    protected void initStaticComponents() {
        JPanel box = new JPanel(new GridLayout(2, 4));
        box.setBorder(BorderFactory.createEtchedBorder());
        box.add(ComponentFactory.createLabel("Разделитель"));

        SettingsManager mgr = SettingsManager.getInstance();
        String prefix = mgr.getProperty(SettingsManager.PREFIX);
        prefixTextField = ComponentFactory.createTextField(prefix);
        box.add(prefixTextField);
        add(box);


        // OK & CANCEL
        JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        saveButton = ComponentFactory.createButton("Сохранить", GUIIcon.SAVE);
        cancelButton = ComponentFactory.createButton("Отмена", GUIIcon.CANCEL);
        buttonsPanel.add(saveButton);
        buttonsPanel.add(cancelButton);
        add(BorderLayout.SOUTH, buttonsPanel);
    }

    @Override
    protected void initListeners() {
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                save();
                dispose();
            }
        });
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
    }

    private void save() {
        SettingsManager mgr = SettingsManager.getInstance();
        String prefix = prefixTextField.getText();
        if (!StringUtils.isEmptyOrNull(prefix) && !prefix.equals(mgr.getProperty(SettingsManager.PREFIX))) {
            mgr.setProperty(SettingsManager.PREFIX, prefix);
            if (mgr.saveToDisk()) {
                JOptionPane.showMessageDialog(getContentPane(),
                        "Настройки успешно сохранены.",
                        "Сохранение",
                        JOptionPane.INFORMATION_MESSAGE
                );
            } else {
                JOptionPane.showMessageDialog(getContentPane(),
                        "Ошибка при сохранении файла настроек.",
                        "Ошибка",
                        JOptionPane.ERROR_MESSAGE
                );
            }
        }
    }
}
